package com.qeegoo.b2bautozityp.moudle.good.view.carmodel;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.databinding.FragGoodsCarmodelOneBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerGoodsFragmentComponent;
import com.qeegoo.b2bautozityp.moudle.base.BaseFragment;
import com.qeegoo.b2bautozityp.moudle.good.entity.MySection;
import com.qeegoo.b2bautozityp.moudle.good.model.CarModelOneBean;
import com.qeegoo.b2bautozityp.moudle.good.viewmodel.carmodel.CarModelOneViewModel;

import java.util.ArrayList;

import javax.inject.Inject;

import base.lib.util.ToastUtils;
import rx.Observable;

/**
 * 王少岩 在 2017/9/25 创建了它
 */

public class CarModelOneFragment extends BaseFragment<FragGoodsCarmodelOneBinding> {
    @Inject
    CarModelOneViewModel mViewModel;

    private String title;
    private String flag = "";
    private ArrayList<MySection<CarModelOneBean.CarSeriesVo>> selectSections = new ArrayList<>();

    public static CarModelOneFragment newInstance(String title, String carLogoId) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("carLogoId", carLogoId);
        CarModelOneFragment fragment = new CarModelOneFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.frag_goods_carmodel_one;
    }

    @Override
    protected void initInjector() {
        DaggerGoodsFragmentComponent.builder().appComponent(getAppComponent()).fragmentComponent(getFragmentComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        title = getArguments().getString("title");
        mViewModel.title.set(title);
        mBinding.setViewModel(mViewModel);
        mViewModel.getListCarSeries(getArguments().getString("carLogoId"));
        initRv();
    }

    private void initRv() {
        mViewModel.getAdapter().addHeaderView(getHeaderView());
        mBinding.rvGoodsCarmodel.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.rvGoodsCarmodel.setAdapter(mViewModel.getAdapter());
        setListener();
    }

    private View getHeaderView() {
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_header, null);
        headerView.setOnClickListener(v -> {
            if (flag.equals("")) {
                flag = "all";
                Observable.from(mViewModel.getAdapter().getData())
                        .filter(vo -> !vo.isHeader)
                        .subscribe(vo -> {
                            vo.t.isSelected = true;
                            selectSections.add(vo);
                        });
                mViewModel.getAdapter().notifyDataSetChanged();
            } else {
                flag = "";
                Observable.from(mViewModel.getAdapter().getData())
                        .filter(vo -> !vo.isHeader)
                        .subscribe(vo -> {
                            vo.t.isSelected = false;
                            selectSections.clear();
                        });
                mViewModel.getAdapter().notifyDataSetChanged();
            }
        });
        return headerView;
    }

    private void setListener() {
        mViewModel.getAdapter().setOnItemClickListener((adapter, view, position) -> {
            MySection<CarModelOneBean.CarSeriesVo> section = ((MySection<CarModelOneBean.CarSeriesVo>) adapter.getItem(position));
            section.t.isSelected = !section.t.isSelected;
            if (section.t.isSelected) {
                selectSections.add(section);
            } else {
                selectSections.remove(section);
            }
            adapter.notifyDataSetChanged();
        });
        mBinding.tvConfirm.setOnClickListener(v -> {
            if (selectSections.size() > 0) {
                StringBuilder sb = new StringBuilder();
                for (MySection<CarModelOneBean.CarSeriesVo> vo : selectSections) {
                    sb.append(vo.t.id).append(",");
                }
                getFragmentManager().beginTransaction()
                        .add(R.id.layout_container, CarModelTwoFragment.newInstance(title, sb.substring(0, sb.length() - 1)))
                        .addToBackStack(null)
                        .commitAllowingStateLoss();
            } else
                ToastUtils.showToast("请选择车系");

        });
    }
}
